/*
 * IdMgr.cpp
 *
 *  Created on: 2011-01-02
 *      Author: kamil
 */

#include "mgr/id/IdMgr.h"
#include <boost/assert.hpp>

std::string getName(ModelId id){
	return IdMgr::instance().getName(id);
}

log4cxx::LoggerPtr IdMgr::logger(log4cxx::Logger::getLogger("idMgr"));

int IdMgr::lastId = 0;

IdMgr::IdMgr() {

}

IdMgr::~IdMgr() {

}

IdMgr & IdMgr::instance(){
	static IdMgr instance;
	return instance;
}

ModelId IdMgr::getId(const std::string & robotName){
	LOG4CXX_DEBUG(logger, "Getting id for: "<<robotName);
	std::map<std::string, ModelId>::iterator it = assignedIds.find(robotName);
	if (it != assignedIds.end()){
		LOG4CXX_DEBUG(logger, "Already assigned id: "<<it->second);
		return it->second;
	}
	// id is not assigned, assign new
	LOG4CXX_DEBUG(logger, "Assigned new id: "<<robotName<<":"<<lastId);
	ModelId newId(lastId);
	assignedIds[robotName] = newId;
	names[newId] = robotName;
	lastId++;
	return newId;
}

std::string IdMgr::getName(ModelId id){
	LOG4CXX_DEBUG(logger, "IdMgr::getName() Model id: "<< id );
	std::map<ModelId, std::string>::iterator it = names.find(id);
	BOOST_ASSERT(it != names.end());
	return it->second;
}
